package com.action;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.config.WebConfig;
import com.hdb.DB;
import com.hdb.TClassification;
import com.hdb.TGood;
import com.hdb.dao.Types;
import com.util.GoodItem;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.*;

@ParentPackage("default")
@Namespace("/good")
@Results({
        @Result(location = "/admin/good/goodManage.jsp"),
        @Result(name = "succeed", location = "/common/succeed.jsp"),
        @Result(name = "edit", location = "/admin/good/goodEditPage.jsp"),
        @Result(name = "adminSearch", location = "/admin/good/goodSearch.jsp"),
        @Result(name = "goodSearch", location = "/view/good/goodSearch.jsp"),
        @Result(name = "goodDetail", location = "/view/good/goodDetail.jsp"),
        @Result(name = "goodList", location = "/view/good/goodList.jsp"),
        @Result(name = "goodByClass", location = "/view/good/goodByClass.jsp"),
        @Result(name = "goodAll", location = "/view/good/goodByClass.jsp"),
})
@InterceptorRef("adminStack")/*设置过滤器*/
public class AdminGoodAction extends BasicAction {
    private int goodId;
    private int goodClassId;
    private String goodName;
    private String goodInfo;
    private String pic;
    private int goodState;
    private int goodType;
    private int goodPrice;
    private int goodLowPrice;
    private int classId;
    private int goodInventory;

    private String message;
    private String path;

    private DB db;
    private int addInventory;

    /*添加商品*/
    @Action("goodAdd")
    public String goodAdd() {
        TGood good = new TGood();
        good.setClassId(classId);
        good.setGoodName(goodName);
        good.setGoodInfo(goodInfo);
        good.setGoodPic(upload());
        good.setGoodState(goodState);
        good.setGoodPrice(goodPrice);
        good.setGoodLowPrice(goodPrice);
        good.setGoodType(Types.good.Normal);
        good.setGoodInventory(goodInventory);
        db.goodDAO.save(good);
        this.setPath("good/goodManage.action");
        return "succeed";
    }
    /*编辑商品*/
    @Action("goodEdit")
    public String goodEdit() {
        TGood good = db.goodDAO.findById(goodId);
        good.setGoodName(goodName);
        good.setGoodInfo(goodInfo);
        String picPath = upload();
        //如果未上传新图片,不在数据库中更新图片路径
        if (!WebConfig.defaultPic.equals(picPath)) {
            good.setGoodPic(picPath);
        }
        good.setGoodPrice(goodPrice);
        good.setGoodLowPrice(goodPrice);
        good.setGoodState(goodState);
        good.setGoodType(Types.good.Normal);
        good.setGoodInventory(goodInventory);
        db.goodDAO.attach(good);
        this.setPath("good/goodManage.action");
        return "succeed";
    }
    /*编辑商品页面*/
    @Action("goodEditPage")
    public String goodEditPage() {
        TGood good = db.goodDAO.findById(goodId);
        List<TClassification> classList = db.classDAO.findAll(0, 40);
        getRequest().put("good", good);
        getRequest().put("classList", classList);
        return "edit";
    }
    /*商品删除*/
    @Action("goodDel")
    public String goodDel() {
        TGood good = db.goodDAO.findById(goodId);
        if (good == null) {
            setMessage("不存在的商品");
            setPath("good/goodManage.action");
            return "succeed";
        }
        boolean res = db.orderGoodDAO.delete("goodId", good.getGoodId()) != -1 && db.goodDAO.delete(good);
        setPath("good/goodManage.action");
        return "succeed";
    }
    /*商品管理*/
    @Action("goodManage")
    public String goodManage() {
        int[] p = getP();
        List<Object[]> goodList = db.goodDAO.join(db.classDAO, "classId", p[0], p[1]);
        List<GoodItem> goodItems = goodList.stream().map(good -> new GoodItem((TGood) good[0], (TClassification) good[1])).collect(Collectors.toList());
        getRequest().put("goodItems", goodItems);
        getPLink(goodItems.size());
        return ActionSupport.SUCCESS;
    }
    //设置低价商品（未完成）
    @Action("goodSetLowPrice")
    public String goodSetLowPrice() {
        TGood good = db.goodDAO.findById(goodId);
        good.setGoodType(Types.good.LowPrice);
        good.setGoodLowPrice(goodLowPrice);
        db.goodDAO.attach(good);
        return ActionSupport.SUCCESS;
    }
    /*增加库存*/
    @Action("goodAddCount")
    public String goodAddCount() {
        TGood good = db.goodDAO.findById(goodId);
        good.setGoodInventory(good.getGoodInventory() + addInventory);
        good.setGoodPrice(goodLowPrice);
        db.goodDAO.attach(good);
        return ActionSupport.SUCCESS;
    }
    /*商品信息*/
    @Action("goodInfo")
    public String goodInfo() {
        Map request = (Map) ServletActionContext.getContext().get("request");
        TGood good = db.goodDAO.findById(goodId);
        request.put("good", good);
        return ActionSupport.SUCCESS;
    }

    public int getGoodState() {
        return goodState;
    }

    public void setGoodState(int goodState) {
        this.goodState = goodState;
    }

    public int getGoodId() {
        return goodId;
    }

    public void setGoodId(int goodId) {
        this.goodId = goodId;
    }

    public int getGoodClassId() {
        return goodClassId;
    }

    public void setGoodClassId(int goodClassId) {
        this.goodClassId = goodClassId;
    }

    public String getGoodName() {
        return goodName;
    }

    public void setGoodName(String goodName) {
        this.goodName = goodName;
    }

    public String getGoodInfo() {
        return goodInfo;
    }

    public void setGoodInfo(String goodInfo) {
        this.goodInfo = goodInfo;
    }

    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic;
    }

    public int getGoodType() {
        return goodType;
    }

    public void setGoodType(int goodType) {
        this.goodType = goodType;
    }

    public int getGoodPrice() {
        return goodPrice;
    }

    public void setGoodPrice(int goodPrice) {
        this.goodPrice = goodPrice;
    }

    public int getGoodLowPrice() {
        return goodLowPrice;
    }

    public void setGoodLowPrice(int goodLowPrice) {
        this.goodLowPrice = goodLowPrice;
    }

    public int getClassId() {
        return classId;
    }

    public void setClassId(int classId) {
        this.classId = classId;
    }

    public int getGoodInventory() {
        return goodInventory;
    }

    public void setGoodInventory(int goodInventory) {
        this.goodInventory = goodInventory;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public DB getDb() {
        return db;
    }

    public void setDb(DB db) {
        this.db = db;
    }

    public int getAddInventory() {
        return addInventory;
    }

    public void setAddInventory(int addInventory) {
        this.addInventory = addInventory;
    }
}
